﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BusyReports;
using BusyReports.ReportingService;
using System.Data.Entity;
using System.Collections.ObjectModel;

namespace BusyReportsGui
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void showLog_Click(object sender, RoutedEventArgs e)
        {
            if (getViewModel().BusySubscription != null)
            {
                ShowLog f = new ShowLog(getViewModel().BusySubscription.log);
                f.Show();
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (
            getViewModel().checkChanges())
            {
                if (MessageBox.Show("there are uncommited changes, save them?", "save changes?", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    save();
                }
            }
            if (getViewModel().checkRunning())
            {
                MessageBox.Show("Looks like there were still reports running", "too bad", MessageBoxButton.OK);
            }
        }

        private void txtFilter_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtFilter.Text != "Filter..")
            {
                getViewModel().Filter = txtFilter.Text;
            }
        }

        private BusyReportsViewModel getViewModel()
        {
            BusyReportsViewModel viewModel = ((BusyReportsViewModel)(this.FindResource("viewModel")));
            return viewModel;
        }

        private void txtFilter_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            tb.Text = string.Empty;
            tb.GotFocus -= txtFilter_GotFocus;
        }


        private void btnNewMailSubscription_Click(object sender, RoutedEventArgs e)
        {
            NewSubscription f = new NewSubscription();
            f.ShowDialog();
            if (f.thePath == null) return;

            try
            {
                getViewModel().newMailSubscription(f.thePath);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (busySubscriptionDataGrid.SelectedItems == null) return;
            if (busySubscriptionDataGrid.SelectedItems.Count == 0) return;

            List<BusySubscription> mySubscriptionsToDelete = new List<BusySubscription>();

            foreach (BusySubscription a in busySubscriptionDataGrid.SelectedItems)
            {
                mySubscriptionsToDelete.Add(a);
            }

            if (MessageBox.Show("delete subscription " + String.Join(",", mySubscriptionsToDelete.Select(a => a.id)), "sure?", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                foreach (BusySubscription a in mySubscriptionsToDelete)
                {
                    try
                    {
                        getViewModel().deleteSubscription(a);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                getViewModel().reloadContext();
            }

        }
        private void btnNewFileSubscription_Click(object sender, RoutedEventArgs e)
        {
            NewSubscription f = new NewSubscription();
            f.ShowDialog();
            if (f.thePath == null) return;

            try
            {
                getViewModel().newFileSubscription(f.thePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            save();
            getViewModel().IsEdit = false;
        }

        private void btnReloadParameters_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("This will clear all parameters and re-insert them, loosing the current values", "sure?", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
            {
                return;
            }
            try
            {
                getViewModel().reloadParameters();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            getViewModel().IsEdit = true;
        }

        private void btnRun_Click(object sender, RoutedEventArgs e)
        {
            if (busySubscriptionDataGrid.SelectedItems == null) return;
            if (busySubscriptionDataGrid.SelectedItems.Count == 0) return;

            List<BusySubscription> mySubscriptionsToRun = new List<BusySubscription>();

            foreach (BusySubscription a in busySubscriptionDataGrid.SelectedItems)
            {
                mySubscriptionsToRun.Add(a);
            }
            getViewModel().run(mySubscriptionsToRun);
        }

        private void btnTestRun_Click(object sender, RoutedEventArgs e)
        {
            if (busySubscriptionDataGrid.SelectedItems == null) return;
            if (busySubscriptionDataGrid.SelectedItems.Count == 0) return;

            List<BusySubscription> mySubscriptionsToTestRun = new List<BusySubscription>();

            foreach (BusySubscription a in busySubscriptionDataGrid.SelectedItems)
            {
                mySubscriptionsToTestRun.Add(a);
            }
            getViewModel().testRun(mySubscriptionsToTestRun);
        }

        private void save()
        {
            try
            {
                getViewModel().save();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Saving Changes");
                getViewModel().reloadContext();
            }
        }

    }
}
